home *** CD-ROM | disk | FTP | other *** search
- /* Main network program - provides both client and server functions */
- #include <stdio.h>
- #include <time.h>
- #ifdef __TURBOC__
- #include <io.h>
- #endif
- #include "global.h"
- #include "config.h"
- #include <stdarg.h>
- #include "files.h"
- #include "mbuf.h"
- #include "socket.h"
- #include "iface.h"
- #include "ftpcli.h"
- #include "telnet.h"
- #include "remote.h"
- #include "session.h"
- #include "cmdparse.h"
- #include "ax25.h"
- #include "kiss.h"
- #include "enet.h"
- #include "timer.h"
- #include "proc.h"
- #include "tty.h"
- #include "daemon.h"
- #include "usock.h"
- #include "netrom.h"
- #include "ip.h"
- #include "tcp.h"
- #include "udp.h"
- #include "hardware.h"
- #include "commands.h"
- #include <dos.h>
- #include <stdlib.h>
- #include <ios1.h>
- #include <proto/dos.h>
-
- #ifdef AMIGA
- #include "amiga/windows.h"
-
- extern int WBorder;
- extern int WResize;
- extern int amiga_winsizes[4];
- extern long amiga_wintype;
- #endif
-
- extern struct cmds Cmds[],Startcmds[],Stopcmds[],Attab[];
- extern int32 Heapsize;
-
- static char Escape = 0x1d; /* default escape character is ^] */
-
- char Badhost[] = "Unknown host %s\n";
- char Badinterface[] = "Interface \"%s\" unknown\n";
-
- char *Hostname;
- char Nospace[] = "No space!!\n"; /* Generic malloc fail message */
-
- struct proc *Cmdpp;
- struct tm *stime;
- int Console;
- struct proc *Display;
- struct session *Command;
- static time_t StartTime; /* Time that NOS was started */
- static int Verbose;
- extern int Attended;
- extern char *Motd;
- char *curftwin;
-
- int main(argc,argv)
- int argc;
- char *argv[];
- {
- char *inbuf,*intmp;
- FILE *fp;
- struct daemon *tp;
- struct mbuf *bp;
- int rootset, c;
-
- StartTime = time(&StartTime); /* NOS Start_Up time */
-
- curftwin = "0023";
-
- rootset = 0;
- while((c = getopt(argc,argv,"s:d:m:l:t:w:h:vbr?")) != EOF){
- switch(c) {
- case '?': /* Options HELP */
- printf("\n AmigaNOS Command-Line Options:\n\n");
- printf(" -l<nnn> Left edge nnn - pixels\n");
- printf(" -t<nnn> Top Edge nnn - pixels\n");
- printf(" -w<nnn> Width nnn - pixels\n");
- printf(" -h<nnn> Height nnn - pixels\n\n");
- printf(" -b Borderless Windows\n");
- printf(" -r Window Resize Gadgets\n\n");
- printf(" -s<nn> Number of sockets\n");
- printf(" -d<sssssss> AmigaNOS Root device/directory\n");
- printf(" -m<nnn> Heap memory size\n\n");
- printf(" -v Verbose flag\n\n");
- exit(0);
- case 'l': /* Left edge */
- amiga_winsizes[0] = atoi(optarg);
- break;
- case 't': /* Top edge */
- amiga_winsizes[1] = atoi(optarg);
- break;
- case 'w': /* Width */
- amiga_winsizes[2] = atoi(optarg);
- break;
- case 'h': /* Height */
- amiga_winsizes[3] = atoi(optarg);
- break;
- case 'b': /* Border */
- WBorder = 0;
- break;
- case 'r': /* Resize */
- WResize = 0;
- break;
- case 's': /* Number of sockets */
- Nusock = atoi(optarg);
- break;
- case 'd': /* Root directory for various files */
- initroot(optarg);
- rootset = 1;
- break;
- case 'm': /* Heap memory size */
- Heapsize = 1024 * atol(optarg);
- break;
- case 'v': /* Verbose flag */
- Verbose = 1;
- break;
- }
- }
- if(!rootset) {
- initroot("TCPIP");
- }
- kinit();
- ioinit(Heapsize);
- sockinit();
- Cmdpp = mainproc("cmdintrp");
- mainlog(-1,"Start NOS v%s", Version);
-
- Sessions = (struct session *)callocw(Nsessions,sizeof(struct session));
- Command = Lastcurr = newsession("command interpreter",COMMAND);
- Display = newproc("display",250,display,0,NULLCHAR,NULL);
- tprintf("KA9Q Internet Protocol Package, v%s\n",Version);
- tprintf("Copyright 1989 by Phil Karn, KA9Q\n");
- fflush(stdout);
-
- if(optind < argc){
- /* Read startup file named on command line */
- if((fp = fopen(argv[optind],READ_TEXT)) == NULLFILE)
- tprintf("Can't read config file %s: %s\n",
- argv[optind],sys_errlist[errno]);
- } else {
- fp = fopen(Startup,READ_TEXT);
- }
- if(fp != NULLFILE){
- inbuf = mallocw(BUFSIZ);
- intmp = mallocw(BUFSIZ);
- while(fgets(inbuf,BUFSIZ,fp) != NULLCHAR){
- strcpy(intmp,inbuf);
- if(Verbose)
- tprintf("%s",intmp);
- if(cmdparse(Cmds,inbuf,NULL) != 0){
- tprintf("input line: %s",intmp);
- }
- }
- fclose(fp);
- free(inbuf);
- free(intmp);
- }
- /* Start background Daemons */
- for(tp=Daemons;;tp++){
- if(tp->name == NULLCHAR)
- break;
- newproc(tp->name,tp->stksize,tp->fp,0,NULLCHAR,NULL);
- }
- /* Now loop forever, processing commands */
- for(;;){
- long currtime;
- time(&currtime);
- stime = localtime(&currtime);
- tprintf("\x0f\x1b[32mNOS <\x1b[33m%02d:%02d - %s\x1b[32m> \x1b[0m",
- stime->tm_hour, stime->tm_min, Hostname);
- usflush(Command->output);
- if(recv_mbuf(Command->input,&bp,0,NULLCHAR,0) != -1){
- (void)cmdparse(Cmds,bp->data,Lastcurr);
- free_p(bp);
- }
- }
- }
-
- /* Keyboard input process */
- void keyboard(i,v1,v2)
- int i;
- void *v1;
- void *v2;
- {
- int c;
- struct mbuf *bp;
-
- /* Keyboard process loop */
- for(;;){
- c = kbread();
- if(c == Escape && Escape != 0)
- c = -2;
- if(c == -2 && Current != Command){
- /* Save current tty mode and set cooked */
- swapscreen(Current,Command);
- Lastcurr = Current;
- Current = Command;
- }
- Current->row = MOREROWS;
- psignal(&Current->row,1);
- if(c >= 0){
- /* If the screen driver was in morewait state, this char
- * has woken him up. Toss it so it doesn't also get taken
- * as normal input. If the char was a command escape,
- * however, it will be accepted; this gives the user
- * a way out of lengthy output.
- */
- if(!Current->morewait
- && (bp = ttydriv(Current,(char)c)) != NULLBUF){
- if(Current->record != NULLFILE
- && Current->ttystate.echo)
- write_p(Current->record,bp);
- send_mbuf(Current->input,bp,0,NULLCHAR,0);
- }
- }
- }
- }
-
- int doexit(argc,argv,p)
- int argc;
- char *argv[];
- void *p;
- {
- mainlog(-1,"Stop NOS v%s", Version);
- reset_all();
- iostop();
- exit(0);
- return 0; /* To satisfy lint */
- }
-
- int dohostname(argc,argv,p)
- int argc;
- char *argv[];
- void *p;
- {
- if(argc < 2)
- tprintf("%s\n",Hostname);
- else {
- if(Hostname != NULLCHAR)
- free(Hostname);
- Hostname = strdup(argv[1]);
- }
- return 0;
- }
-
- int dohelp(argc,argv,p)
- int argc;
- char *argv[];
- void *p;
- {
- register struct cmds *cmdp;
- int i;
- char buf[77];
-
- if(*argv[0] == '?' ) {
- tprintf("Main commands:\n");
- memset(buf,' ',sizeof(buf));
- buf[75] = '\n';
- buf[76] = '\0';
- for(i=0,cmdp = Cmds;cmdp->name != NULL;cmdp++,i = (i+1)%5) {
- strncpy(&buf[i*15],cmdp->name,strlen(cmdp->name));
- if(i == 4){
- tprintf(buf);
- memset(buf,' ',sizeof(buf));
- buf[75] = '\n';
- buf[76] = '\0';
- }
- }
- if(i != 0)
- tprintf(buf);
- return 0;
- } else {
- buf[0] = '\0';
- if(argc > 1)
- for(i=0; Cmds[i].name != NULLCHAR; ++i)
- if(!strncmp(Cmds[i].name,
- argv[1],strlen(argv[1]))) {
- sprintf(buf,"ARUN LESS %s/main.%s NOIO",
- Helpdir,Cmds[i].name);
- if (!Execute(buf, 0, 0)) {
- return (FILE *)NULL;
- }
- break;
- }
- return 0;
- }
- }
-
- /* Attach an interface
- * Syntax: attach <hw type> <I/O address> <vector> <mode> <label> <bufsize> [<speed>]
- */
- int doattach(argc,argv,p)
- int argc;
- char *argv[];
- void *p;
- {
- return subcmd(Attab,argc,argv,p);
- }
-
- /* Manipulate I/O device parameters */
- int doparam(argc,argv,p)
- int argc;
- char *argv[];
- void *p;
- {
- register struct iface *ifp;
-
- if((ifp = if_lookup(argv[1])) == NULLIF){
- tprintf(Badinterface,argv[1]);
- return 1;
- }
- if(ifp->ioctl == NULLFP){
- tprintf("Not supported\n");
- return 1;
- }
- /* Pass rest of args to device-specific code */
- return (*ifp->ioctl)(ifp,argc-2,argv+2);
- }
-
- /* Display or set IP interface control flags */
- int domode(argc,argv,p)
- int argc;
- char *argv[];
- void *p;
- {
- register struct iface *ifp;
-
- if((ifp = if_lookup(argv[1])) == NULLIF) {
- tprintf(Badinterface, argv[1]);
- return 1;
- }
- if(argc < 3){
- tprintf("%s: %s\n",ifp->name,
- (ifp->flags & CONNECT_MODE) ? "VC mode" : "Datagram mode");
- return 0;
- }
- switch(argv[2][0]){
- case 'v':
- case 'c':
- case 'V':
- case 'C':
- ifp->flags = CONNECT_MODE;
- break;
- case 'd':
- case 'D':
- ifp->flags = DATAGRAM_MODE;
- break;
- default:
- tprintf("Usage: %s [vc | datagram]\n",argv[0]);
- return 1;
- }
- return 0;
- }
-
- int doescape(argc,argv,p)
- int argc;
- char *argv[];
- void *p;
- {
- if(argc < 2)
- tprintf("0x%x\n",Escape);
- else
- Escape = *argv[1];
- return 0;
- }
-
- /* Generate system command packet. Synopsis:
- * remote [-p port#] [-k key] [-a hostname] <hostname> reset|exit|kickme
- */
- int doremote(argc,argv,p)
- int argc;
- char *argv[];
- void *p;
- {
- struct sockaddr_in fsock;
- int s,c;
- char *data,x;
- int16 port,len;
- char *key = NULLCHAR;
- int klen;
- int32 addr = 0;
- char *cmd,*host;
-
- port = IPPORT_REMOTE; /* Set default */
- optind = 1; /* reinit getopt() */
- while((c = getopt(argc,argv,"a:p:k:s:")) != EOF){
- switch(c){
- case 'a':
- addr = resolve(optarg);
- break;
- case 'p':
- port = atoi(optarg);
- break;
- case 'k':
- key = optarg;
- klen = strlen(key);
- break;
- case 's':
- Rempass = strdup(optarg);
- return 0; /* Only set local password */
- }
- }
- if(optind > argc - 2){
- tprintf("Insufficient args\n");
- return -1;
- }
- host = argv[optind++];
- cmd = argv[optind];
- if((s = socket(AF_INET,SOCK_DGRAM,0)) == -1){
- tprintf("socket failed\n");
- return 1;
- }
- len = 1;
- /* Did the user include a password or kickme target? */
- if(addr != 0)
- len += sizeof(int32);
-
- if(key != NULLCHAR)
- len += klen;
-
- if(len == 1)
- data = &x;
- else
- data = mallocw(len);
-
- fsock.sin_family = AF_INET;
- fsock.sin_addr.s_addr = resolve(host);
- fsock.sin_port = port;
-
- switch(cmd[0]){
- case 'r':
- data[0] = SYS_RESET;
- if(key != NULLCHAR)
- strncpy(&data[1],key,klen);
- break;
- case 'e':
- data[0] = SYS_EXIT;
- if(key != NULLCHAR)
- strncpy(&data[1],key,klen);
- break;
- case 'k':
- data[0] = KICK_ME;
- if(addr != 0)
- put32(&data[1],addr);
- break;
- default:
- tprintf("Unknown command %s\n",cmd);
- goto cleanup;
- }
- /* Form the command packet and send it */
- if(sendto(s,data,len,0,(char *)&fsock,sizeof(fsock)) == -1){
- tprintf("sendto failed: %s\n",sys_errlist[errno]);
- goto cleanup;
- }
- cleanup:
- if(data != &x)
- free(data);
- close_s(s);
- return 0;
- }
-
- int doless(argc,argv,p)
- int argc;
- char *argv[];
- void *p;
- {
- char buf[81];
-
- sprintf(buf,"ARUN LESS %s NOIO",argv[1]);
- if (!Execute(buf, 0, 0)) {
- return (FILE *)NULL;
- }
- return 0;
- }
-
- /* Log messages of the form
- * 00:00:00 44.64.0.7:1003 open FTP
- */
- void mainlog(int s,char *fmt, ...)
- {
- va_list ap;
- char *cp, *da, *mo, *yr, *ti, ML[128];
- long t;
- int i;
- struct sockaddr fsocket;
- FILE *fp;
-
- time(&t);
- cp = ctime(&t);
- rip(cp);
-
- strtok(cp," "); /* Day */
- mo = strtok(NULL," "); /* Month */
- da = strtok(NULL," "); /* Date */
- ti = strtok(NULL," "); /* Time */
- yr = strtok(NULL," "); /* Year */
-
- sprintf(ML, "%s/%s-%s-%s", LogsDir, mo, da, yr);
-
- if((fp = fopen(ML,APPEND_TEXT)) == NULLFILE)
- return;
-
- i = SOCKSIZE;
-
- fprintf(fp,"%s ",ti);
- if(getpeername(s,(char *)&fsocket,&i) != -1)
- fprintf(fp," %s",psocket(&fsocket));
- fprintf(fp," - ");
- va_start(ap,fmt);
- vfprintf(fp,fmt,ap);
- va_end(ap);
- fprintf(fp,"\n");
- fclose(fp);
- }
-
- int dosource(argc,argv,p)
- int argc;
- char *argv[];
- void *p;
- {
- int linenum = 0;
- char *inbuf,*intmp;
- FILE *fp;
-
- /* Read command source file */
- if((fp = fopen(argv[1],READ_TEXT)) == NULLFILE){
- tprintf("Can't read source file %s: %s\n",
- argv[1],sys_errlist[errno]);
- return 1;
- }
-
- inbuf = malloc(BUFSIZ);
- intmp = malloc(BUFSIZ);
- while(fgets(inbuf,BUFSIZ,fp) != NULLCHAR){
- strcpy(intmp,inbuf);
- linenum++;
- if(Verbose)
- tprintf("%s",intmp);
- if(cmdparse(Cmds,inbuf,NULL) != 0){
- tprintf("*** file \"%s\", line %d: %s\n",
- argv[1],linenum,intmp);
- }
- }
- fclose(fp);
- free(inbuf);
- free(intmp);
- return 0;
- }
-
- dostatus(argc,argv,p)
- int argc;
- char *argv[];
- void *p;
- {
- time_t nowtime, elapsedtime;
- unsigned int days,hrs,mins,secs;
-
- nowtime = time(&nowtime); /* current time */
- elapsedtime = nowtime - StartTime; /* nos elapsed time */
-
- tprintf("KA9Q Internet Protocol Package, v%s\n\n",Version);
- tprintf("The system time is %s", ctime(&nowtime));
- tprintf("NOS was started on %s\n", ctime(&StartTime));
- secs = elapsedtime % 60;
- elapsedtime = elapsedtime / 60;
- mins = elapsedtime % 60;
- elapsedtime = elapsedtime / 60;
- hrs = elapsedtime % 24;
- elapsedtime = elapsedtime / 24;
- days = elapsedtime;
- tprintf("Elapsed time => %u days: %02u hours: %02u minutes: %02u seconds.\n\n",days,hrs,mins,secs);
- tprintf("The station is currently %sttended.\n", Attended ? "A" : "Una");
- tprintf("The 'Message Of The Day' is ");
- if(Motd != NULLCHAR)
- tprintf("\n%s",Motd);
- else
- tprintf("not set!\n");
- #ifdef __TURBOC__
- dofstat(); /* print status of open files */
- #endif
- tprintf("\nNOS configuration information.\ncontaining ");
- #ifdef SERVERS
- tprintf(" TCP servers,");
- #endif
- #ifdef POP
- tprintf(" POP2,");
- #endif
- #ifdef NNTP
- tprintf(" NNTP,");
- #endif
- #ifdef RIP
- tprintf(" RIP,");
- #endif
- #ifdef RSPF
- tprintf(" RSPF,");
- #endif
- #ifdef BOOTP
- tprintf(" BOOTP,");
- #endif
- #ifdef HOPCHECK
- tprintf(" HOP,");
- #endif
- #ifdef MAILBOX
- tprintf(" MAILBOX");
- #endif
- #ifdef ASY
- tprintf("\nGeneric async interface\n");
- #endif
- #ifdef SLIP
- tprintf("SLIP async interface\n");
- #endif
- #ifdef KISS
- tprintf("KISS async interface\n");
- #endif
- #ifdef NRS
- tprintf("NET/ROM async interface\n");
- #endif
- #ifdef NETROM
- tprintf("NET/ROM network interface\n");
- #endif
- #ifdef DIGGER
- tprintf("Digger by WA2ZZX/G1YYH\n");
- #endif
- #ifdef CHATNODE
- tprintf("ChatNode by DK5SG/G1YYH\n");
- #endif
- #ifdef PPP
- tprintf("Point to Point network interface\n");
- #endif
- #ifdef VJCOMPRESS
- tprintf("Van Jacobson compression on SLIP/PPP\n");
- #endif
- #ifdef FORTH
- tprintf("Forth programming language\n");
- #endif
- return 0;
- }
-
- /* Set up FTP/Download window */
- int doftpdown(argc,argv,p)
- int argc;
- char *argv[];
- void *p;
- {
- if(argc == 1)
- tprintf("Current FTP/Download Window = %s\n", curftwin);
- else {
- strcpy(curftwin,argv[1]);
- tprintf("New FTP/Download Window = %s \n", curftwin);
- }
- return 0;
- }
-